package ip89

import (
	"github.com/ruoklive/proxypool/internal/ipgetter/base"
	"github.com/ruoklive/proxypool/pkg/register"
	"go.uber.org/zap"
	"io"
	"io/ioutil"

	"github.com/ruoklive/proxypool/pkg/models"
	"strings"
)

func init() {
	register.Add(func() register.IPGetter {
		return New()
	})
}

type IPGetter struct {
	base.Base
}

func New() *IPGetter {
	g := &IPGetter{}
	g.Base = base.NewBase("http://www.89ip.cn/tqdl.html?api=1&num=100&port=&address=%E7%BE%8E%E5%9B%BD&isp=",g.Parse,"IP89")
	return g
}

//IP89 get ip from www.89ip.cn
func (g *IPGetter) Parse(reader io.ReadCloser) (result []*models.IP) {
	var ExprIP = g.IPRegexp()
	body, _ := ioutil.ReadAll(reader)
	bodyIPs := string(body)
	ips := ExprIP.FindAllString(bodyIPs, 100)

	for index := 0; index < len(ips); index++ {
		ip := models.NewIP()
		ip.Data = strings.TrimSpace(ips[index])
		ip.Type1 = "http"
		g.Info("IP", zap.String("ip",ip.String()))
		result = append(result, ip)
	}

	g.Info("done.")
	return
}
